/*
 *  CEFileUtils.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "lid_lib/src/file_managment/LLFileUtils.h"

#include "lid_lib/src/strings_and_localization/LLStringUtils.h"

#include <FBase.h>
#include <FIo.h>

#include <stdio.h>

using namespace std;
using namespace Osp::Base;
using namespace Osp::Io;

namespace lidlib {

/////////////////////////////////////////////////////
//
/** CEFileUtils
*
*/
/////////////////////////////////////////////////////

std::vector<std::string> LLFileUtils::getFileList(const std::string& fullDirectoryPath)
{
	std::vector<std::string> resultList;

    String dirName = fullDirectoryPath.c_str();
    Directory* pDir;
    DirEnumerator* pDirEnum;
    result r = E_SUCCESS;

    //------------------------------------------------
    // Directory entry traversal example
    //------------------------------------------------
    pDir = new Directory(); // Allocates the %Directory instance
    r = pDir->Construct(dirName);
    if (IsFailed(r))
        return resultList;

    // Reads all the directory entries
    pDirEnum = pDir->ReadN();
    if(!pDirEnum)
    	return resultList;

    // Loops through all the directory entries
    while(pDirEnum->MoveNext() == E_SUCCESS)
    {
        DirEntry entry = pDirEnum->GetCurrentDirEntry();
        if (entry.IsDirectory())
            continue;
        std::wstring wname = entry.GetName().GetPointer();
        std::string name = utf16_wstring_to_utf8_string(wname);
        resultList.push_back(name);
    }

    delete pDirEnum;
    pDirEnum = null;
    // Releases the %Directory instance.
    // The opened directory is closed automatically when the destructor of the %Directory class is invoked.
    delete pDir;
    pDir = null;

    return resultList;
}

std::vector<std::string> LLFileUtils::getDirectoryList(const std::string& fullDirectoryPath)
{
	std::vector<std::string> resultList;

    String dirName = fullDirectoryPath.c_str();
    Directory* pDir;
    DirEnumerator* pDirEnum;
    result r = E_SUCCESS;

    //------------------------------------------------
    // Directory entry traversal example
    //------------------------------------------------
    pDir = new Directory(); // Allocates the %Directory instance
    r = pDir->Construct(dirName);
    if (IsFailed(r))
        return resultList;

    // Reads all the directory entries
    pDirEnum = pDir->ReadN();
    if(!pDirEnum)
    	return resultList;

    // Loops through all the directory entries
    while(pDirEnum->MoveNext() == E_SUCCESS)
    {
        DirEntry entry = pDirEnum->GetCurrentDirEntry();
        if (!entry.IsDirectory())
        	continue;
        std::wstring wname = entry.GetName().GetPointer();
        std::string name = utf16_wstring_to_utf8_string(wname);
        resultList.push_back(name);
    }

    delete pDirEnum;
    pDirEnum = null;
    // Releases the %Directory instance.
    // The opened directory is closed automatically when the destructor of the %Directory class is invoked.
    delete pDir;
    pDir = null;

    return resultList;
}

string LLFileUtils::getWriteablePath()
{
	return "/Home/";
}

bool  LLFileUtils::isFileExists(const std::string& fileName)
{
	FILE *fp = fopen(fileName.c_str(), "rb");
	if(fp == NULL)
		return false;
	fclose(fp);
	return true;
}

unsigned char*  LLFileUtils::getFileData(const std::string& fileName, unsigned long* pSize)
{
    // read the file from hardware
    FILE *fp = fopen(fileName.c_str(), "rb");
    if (fp == NULL)
		return NULL;

    fseek(fp,0,SEEK_END);
    *pSize = ftell(fp);

    fseek(fp,0,SEEK_SET);
    unsigned char* pBuffer = new unsigned char[*pSize];
    *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
    fclose(fp);

    return pBuffer;
}

std::string  LLFileUtils::fullPathFromRelativePath(const std::string& relativePath)
{
    std::string ret;
    if (!relativePath.empty() && '/' == relativePath[0])
    {
        ret = relativePath;
    }
    else
    {
        ret = LLFileUtils::getResourcePath();
        ret += "/";
        ret += relativePath;
    }
	return ret;
}

std::string  LLFileUtils::fullPathFromRelativeFile(const std::string& filename, const std::string& relativeFile)
{
	std::string fullFile = fullPathFromRelativePath(relativeFile);
	std::string ret = fullFile.substr(0, fullFile.find_last_of("/") + 1);
	ret += filename;

	return ret;
}

}; // end namespace lidlib
